package com.ktjy.dto;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

public class LoginUser implements UserDetails {
    private Long userId;
    private String password;
    private String username;
    private Set<String> perms;
    private String avatar;
    private String realName;
    private Long deptId;

    public LoginUser() {
    }

    public LoginUser(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public LoginUser(String username, String password, Set<String> perms) {
        this.username = username;
        this.password = password;
        this.perms = perms;
    }

    public LoginUser(Long userId, String username, String password, Set<String> perms) {
        this.userId = userId;
        this.username = username;
        this.password = password;
        this.perms = perms;
    }

    public LoginUser(Long userId, String username, String password, String realName, Long deptId, Set<String> perms) {
        this.userId = userId;
        this.password = password;
        this.username = username;
        this.perms = perms;
        this.realName = realName;
        this.deptId = deptId;
    }

    public Long getUserId() {
        return userId;
    }


    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Set<String> getPerms() {
        return perms;
    }

    public void setPerms(Set<String> perms) {
        this.perms = perms;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    public Long getDeptId() {
        return deptId;
    }

    public void setDeptId(Long deptId) {
        this.deptId = deptId;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @JsonIgnore
    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        Set<GrantedAuthority> set = new HashSet<>();
        if (perms != null && perms.size() > 0) {
            for (String perm : perms) {
                SimpleGrantedAuthority authority = new SimpleGrantedAuthority(perm);
                set.add(authority);
            }
        }
        return set;
//
//        return null;
    }

    @JsonIgnore
    @Override
    public String getPassword() {
        return this.password;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isEnabled() {
        return true;
    }

}
